package com.authsphere.security.configurer.password.filter;

import com.authsphere.common.constant.Constants;
import com.authsphere.security.common.AbstractRequestBodyAuthenticationFilter;
import com.authsphere.security.configurer.password.entity.UsernamePasswordEntity;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/11 11:32
 **/
public class UsernamePasswordRequestBodyAuthenticationFilter extends AbstractRequestBodyAuthenticationFilter<UsernamePasswordEntity> {
    private final static String DEFAULT_PROCESS_URL = "/pwd/login";
    private final static String DEFAULT_MODULE_TYPE = "password";

    public UsernamePasswordRequestBodyAuthenticationFilter() {
        super(DEFAULT_MODULE_TYPE, DEFAULT_PROCESS_URL, UsernamePasswordEntity.class);
    }

    public UsernamePasswordRequestBodyAuthenticationFilter(String moduleType) {
        super(moduleType, DEFAULT_PROCESS_URL, UsernamePasswordEntity.class);
    }

    @Override
    protected Authentication buildAuthenticationToken(HttpServletRequest request, HttpServletResponse response, UsernamePasswordEntity requestJsonBody) {
        return new UsernamePasswordAuthenticationToken(requestJsonBody.getUsername(), requestJsonBody.getPassword());
    }

    @Override
    protected void beforeAuthentication(HttpServletRequest request, HttpServletResponse response, Object data) {
        super.beforeAuthentication(request, response, data);
        request.setAttribute(Constants.OPERATE_NAME, "用户密码登录");
    }
}
